function drag(dragNode, moveNode, callBack) {
  if (!moveNode) {
    moveNode = dragNode;
  }
  let elementPoint = {x: 0, y: 0}
  let startPoint = {x: 0, y: 0}
  let nodePoint = moveNode.getBoundingClientRect()
  let parentPoint = moveNode.parentNode.getBoundingClientRect()
  // if (parentPoint.width > nodePoint.width) {
  //   return false
  // }
  // console.log('nodePoint', getOffsetLeft(moveNode));
  let maxDistance = parentPoint.width - nodePoint.width
  dragNode.onmousedown = function (ev) {
    // console.log('down', this.offsetLeft, this.left, this.style);
    // console.log('nodePoint', getOffsetLeft(moveNode));
    ev = ev || event
    let childNod = ev.path.find(item => {
      return item.className == 'form_item' || item.className == 'main' || item.className == 'main ship_main' || item.className == 'el-input' || item.className == 'el-button el-button--primary' || item.className == 'dia_title'
    })
    if (!childNod) {
      ev.stopPropagation()
      elementPoint.x = getOffsetLeft(moveNode);
      startPoint.x = ev.clientX
      elementPoint.y = getOffsetTop(moveNode);
      startPoint.y = ev.clientY
      if (dragNode.setCapture) {
        dragNode.setCapture()
      }
      document.onmousemove = function (ev) {
        ev = ev || event
        let moveLeft = elementPoint.x + (ev.clientX - startPoint.x)
        let moveTop = elementPoint.y + (ev.clientY - startPoint.y)
        // console.log(moveLeft)
        // if(moveLeft < maxDistance){
        //   moveLeft = maxDistance
        // }else if (moveLeft > 0){
        //   moveLeft = 0
        // }
        moveNode.style.left = moveLeft + 'px'
        moveNode.style.top = moveTop + 'px'
      }

      document.onmouseup = function (ev) {
        document.onmousemove = document.onmouseup = null
        elementPoint.x = moveNode.offsetLeft
        if (document.releaseCapture) {
          document.releaseCapture()
        }
      }
    }

  }
}

function getOffsetTop(obj) {
  var tmp = obj.offsetTop;
  var val = obj.offsetParent;
  while (val != null) {
    tmp += val.offsetTop;
    val = val.offsetParent;
  }
  return tmp;
}

function getOffsetLeft(obj) {
  var tmp = obj.offsetLeft;
  var val = obj.offsetParent;
  while (val != null) {
    tmp += val.offsetLeft;
    val = val.offsetParent;
  }
  return tmp;
}

export default drag
